<?php

class Model
{
	private $model;
    protected $reader;
    public $select_db = DEFAULT_DB_NAME;

    public function __construct($model)
    {
		$this->model = $model;
        $this->reader = $this->reader ? $this->reader : $this->model;
    }

    static final public function getInstance($model)
    {
        $model = ucwords($model);
        static $instances = array();

        if (! isset($instances[$model]))
        {
            $file = strtolower($model);
            if (file_exists(MODEL_PATH . $file . '.php')){
                include_once MODEL_PATH . $file . '.php';
			    $classname = $model.'Model';
                $instances[$model] = new $classname($model);
            }else{
                p('没有找到Model文件：'.MODEL_PATH.$file.'.php');
            }
        }
        return $instances[$model];
    }
    
    final public function db($db = DEFAULT_DB_NAME)
    {
        if($db != DEFAULT_DB_NAME){
            $this->select_db = $db;
        }
        return DBFactory::getInstance($this->select_db);
    }

    final public function pager($curr_page = 1, $per_page = 10, $db_name = DEFAULT_DB_NAME)
    {
        include_once LIB_PATH . 'pager.php';
        return new SnowPager($this->db($db_name), $curr_page, $per_page);
    }

    final public function input(){
        return Input::getInstance();
    }
    
    final public function session(){
        return Session::getInstance();
    }
}

?>